package com.uls.ldcm.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import com.uls.ldcm.service.ILaborersService;
import com.uls.system.service.ISysUserService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ldcm.domain.Salarypayments;
import com.uls.ldcm.service.ISalarypaymentsService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 薪资发放管理Controller
 * 
 * @author gw
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/ldcm/salarypayments")
public class SalarypaymentsController extends BaseController
{
    @Autowired
    private ISalarypaymentsService salarypaymentsService;

    @Autowired
    private ILaborersService laborersService;

    @Autowired
    private MinioUtils minioUtils;


    @Autowired
    private MinioProp minioProp;


    /**
     * 查询薪资发放管理列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:list')")
    @GetMapping("/list")
    public TableDataInfo list(Salarypayments salarypayments)
    {
        startPage();
        List<Salarypayments> list = salarypaymentsService.selectSalarypaymentsList(salarypayments);
        for (Salarypayments salarypayments1:list){
            salarypayments1.setName(laborersService.selectLaborersById(salarypayments1.getLaborerId()).getName());
        }
        return getDataTable(list);
    }

    /**
     * 导出薪资发放管理列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:export')")
    @Log(title = "薪资发放管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Salarypayments salarypayments)
    {
        List<Salarypayments> list = salarypaymentsService.selectSalarypaymentsList(salarypayments);
        ExcelUtil<Salarypayments> util = new ExcelUtil<Salarypayments>(Salarypayments.class);
        util.exportExcel(response, list, "薪资发放管理数据");
    }

    /**
     * 获取薪资发放管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:query')")
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        return success(salarypaymentsService.selectSalarypaymentsByPaymentId(paymentId));
    }

    /**
     * 新增薪资发放管理
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:add')")
    @Log(title = "薪资发放管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Salarypayments salarypayments)
    {
        return toAjax(salarypaymentsService.insertSalarypayments(salarypayments));
    }

    /**
     * 修改薪资发放管理
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:edit')")
    @Log(title = "薪资发放管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Salarypayments salarypayments)
    {
        return toAjax(salarypaymentsService.updateSalarypayments(salarypayments));
    }

    /**
     * 删除薪资发放管理
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:remove')")
    @Log(title = "薪资发放管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        return toAjax(salarypaymentsService.deleteSalarypaymentsByPaymentIds(paymentIds));
    }

    /**
     * 上传合同文件
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }

    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:down')")
    @PostMapping("/fileDown")
    public  void imageDown(String fileName, HttpServletResponse response){
        minioUtils.download(minioProp.getBucket(),fileName,response);
    }

    /**
     * 提交工资申请
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:salarySubmit')")
    @Log(title = "薪资发放管理", businessType = BusinessType.EXPORT)
    @GetMapping("/submitSalary/{paymentId}")
    public AjaxResult submitSalary(@PathVariable Long paymentId)
    {
        return  toAjax(salarypaymentsService.submit(paymentId, getUserId()));
    }

    /**
     * 查询未审批相关信息列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:listNo')")
    @GetMapping("/listNo")
    public TableDataInfo listNo(Salarypayments salarypayments)
    {
        startPage();
        List<Salarypayments> list=salarypaymentsService.noFinishTask(getUserId(),"LbSalaryTask",salarypayments);
        for (Salarypayments salarypayments1:list){
            salarypayments1.setName(laborersService.selectLaborersById(salarypayments1.getLaborerId()).getName());
        }
        return getDataTable(list);
    }

        /**
     * 获取审批薪资发放管理详细信息
     */
    @GetMapping(value = "/getSalaryComment/{paymentId}")
    public AjaxResult getInfoComment(@PathVariable("paymentId") Long paymentId)
    {
        Salarypayments salarypayments = salarypaymentsService.selectSalarypaymentsByPaymentId(paymentId);
        salarypayments.setTaskCommentVoList(salarypaymentsService.getTaskCommentVoList(salarypayments.getFlowInstantId()));
        return success(salarypayments);
    }

    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:approval')")
    @Log(title = "查询未审批相关学生信息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/editnoapprove")
    public AjaxResult editSaveNoAuthData(@RequestBody Salarypayments payment)
    {
        salarypaymentsService.completeTask(payment.getFlowInstantId(),payment.getAgree(),getUserId(),payment.getComment(),payment,payment.getManner());
        return  toAjax(200);
    }

        /**
     * 查询审批完成相关信息列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:salarypayments:listFinished')")
    @GetMapping("/listFinished")
    public TableDataInfo listFinished(Salarypayments salarypayments)
    {
        // TODO 数据没有分页成功
        startPage();
        List<Salarypayments> list=salarypaymentsService.finishTask(getUserId(),"LbSalaryTask",salarypayments);
        for (Salarypayments salarypayments1:list){
            salarypayments1.setName(laborersService.selectLaborersById(salarypayments1.getLaborerId()).getName());
        }
        return getDataTable(list);
    }
}
